# -*- coding: utf-8 -*-
"""
Created on Mon Apr 24 19:55:15 2023

@author: 52224
"""
#=****载入运行所需库****=#
import sys
import pyvisa as pv
import numpy as np
import time
import pandas as pd
import matplotlib.pyplot as plt
sys.path.append('D:\\Seafile\\Yijun_Li\\Auto_Test_Project\\MicroTech_Cascade_Summit_12k\\')
sys.path.append('D:\\Seafile\\Yijun_Li\\Auto_Test_Project\\B1500\\')
sys.path.append('D:\\Seafile\\Yijun_Li\\Auto_Test_Project\\B2200\\')
from Die_map_Liyijun_CFET_CPU import sub_die_row,X_axis,Y_axis
from Probe_Station_API import MoveChuck,ReadChuckPosition
from B1500_IDVG_IDVD import Transistor_Test_IDVG
from B2200 import Setup_Path,Save_Path,Load_Path
#=****定义测试程序，包含开关矩阵切换和测试****=#
def IDVG_TEST_NMOS(B1500):
    VG_Unit='1'
    Start_VG=-3.0
    End_VG=3.0
    nop_VG=201
    VG_setup=[VG_Unit,Start_VG,End_VG,nop_VG]
    VS_Unit='2'
    VS_comp=1e-2
    VS_setup=[VS_Unit,VS_comp]
    VD_Voltage=1.0
    VD_comp=1e-2
    VD_Unit1='3'
    VD_Unit2='4'
    VD_Unit3='5'
    VD_Unit4='6'
    VD_Unit5='7'
    VD_setup=[VD_Voltage,VD_comp,
              VD_Unit1,VD_Unit2,VD_Unit3,VD_Unit4,VD_Unit5]
    return Transistor_Test_IDVG(B1500,VG_setup,VS_setup,VD_setup)

#=****连接探针台****=#
Probe_Station_Address='ASRL9::INSTR'
rm=pv.ResourceManager()
Probe_Station=rm.open_resource(Probe_Station_Address)
Probe_Station.timeout=1000
#=****B1500和B2200****=#
rm=pv.ResourceManager()
B2201A_address='GPIB0::22::INSTR'
B1500_address='GPIB0::18::INSTR'
B2200 = rm.open_resource(B2201A_address)
B1500=rm.open_resource(B1500_address)
B1500.timeout=600000
B1500.write('*RST')
B1500.write('CN')
Setup_Path(B2200,[102,106,110,114,118,201,205,209,213,217,303,407,511,615,719])
Save_Path(B2200,1)
#=****清空探针台读寄存器****=#
while(1):
    try:
        Probe_Station.read_raw()
    except:
        Probe_Station.timeout=6000
        break
#=****请在开始测试前完成以下操作****=#
#1.调整旋转，用M或者A或者手动
#2.设置Home Die
#3.Test Run(注释掉测试行和Sub_Die)
 #=****请在开始测试前完成以下操作****=#      

#=****判断是否正确设置Home Die，并在测试开始前将探针台移动到Home Die****=#  
Home_position=ReadChuckPosition(Probe_Station,'Home')
Home_position=Home_position[0:2]
if abs(sum(Home_position)) > 10:
    print('Pleas set the home die and move chuck to home position!')
    sys.exit() 
#=****主测试程序****=#
for i in range(np.size(X_axis,0)):
    for j in range(np.size(X_axis,1)):
        #=****跳过实际不存在的Die****=#  
        if X_axis[i,j] == -999999:
            continue
        if Y_axis[i,j] == -999999:
            continue
        #=****Die移动****=#  
        X=X_axis[i,j]
        Y=Y_axis[i,j]
        #=****Sub Die移动****=#
        print('Move to die ('+str(i+1)+','+str(j+1)+')'+' with error of (um): '+str(MoveChuck(Probe_Station, X, Y, 'Home', 100)))
        for x in range(10):
            t0=time.time()
            sub_die=x*2
            if x == 0:
                t1=time.time()
                print('   Move to Sub_die 2')
            else:
                print('   Move to Sub_die '+str(sub_die+2)+' with error of (um) : '+str(MoveChuck(Probe_Station, 0, sub_die_row*2, 'Relative', 100)))
            #=****主测试程序****=#
            Load_Path(B2200,1)
            data=IDVG_TEST_NMOS(B1500)
            
            fig=plt.figure()
            x=np.float32(data[1:,0])
            x=x.reshape([np.size(x),1])
            y=abs(np.float32(data[1:,[5,7,9,11,13]]))
            plt.plot(x,y)
            plt.show()
            
            data=pd.DataFrame(data[1:,:],columns=data[0,:])
            data.to_csv('D:\\Seafile\\Yijun_Li\\Auto_Test_Project\\data\\20230427_YHM_New\\Die_'+str(i+1)+'_'+str(j+1)+'__'+str(sub_die+2)+'.csv',sep=',',index=None)
            
            t1=time.time()
            print('      Test time: '+'%.3f'%(t1-t0)+'s.')   
               